package com.yphone.ui.login.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

import com.iiordanov.bVNC.App;
import com.zzkj.yunphone.R;
import com.yphone.tools.Constants;
import com.yphone.tools.ToastUtil;
import com.yphone.utils.RegexUtils;

public class PwdView extends EditText {
    private TextWatcher mTextWatcher = new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
            if (!RegexUtils.isMatch(Constants.PWD_MATCHING, s.toString())) {
                PwdView.this.setError(App.getContext().getString(R.string.mmgssryw));
            }
        }
    };

    public PwdView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public PwdView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PwdView(Context context) {
        super(context);
        init();
    }

    private void init() {
        addTextChangedListener(this.mTextWatcher);
    }

    public String getPwd() {
        String pwd = getText().toString().trim();
        if (pwd.equals("")) {
            ToastUtil.showMidToast(getContext(), getResources().getString(R.string.login_pwd_empty));
            return "";
        } else if (pwd.length() >= 6) {
            return pwd;
        } else {
            ToastUtil.showMidToast(getContext(), getResources().getString(R.string.login_pwd_six));
            return "";
        }
    }

    public String getPassword() {
        return getText().toString().trim();
    }
}